/**
 * @file axios的封装
 * @author its-wild(https://gitee.com/its-wild)
 * @date 2021/07/13
 */
import { App } from 'vue'
import axios from 'axios'
import { Modal, notification } from 'ant-design-vue'
import { Storage } from './storage'

let storage: Storage

// 创建 axios 实例
const service = axios.create({
  /* headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'}, */
  timeout: 60 * 1000 // 请求超时时间
})

// 在发送请求之前做某件事(添加请求拦截器)
service.interceptors.request.use(
  config => {
    const token = storage.get('token')
    if (token) {
      config.headers = {
        'Authorization': token, // 让每个请求默认携带自定义 token，如果请求自己设置有就会覆盖默认的这个， 请根据实际情况自行修改
        ...config.headers
      }
    }
    if (config.method === 'get') {
      config.params = {
        _: (new Date().getTime() / 1000).toFixed(0), //get方法加时间标识，1秒外防止缓存
        ...config.params
      }
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 返回状态判断(添加响应拦截器)
service.interceptors.response.use(
  response => {
    // 不需要处理数据
    const noProcessingData = response.config.params?._noProcessingData
    if (noProcessingData) {
      return response.data
    }
    const code = response.data.code
    const msg = response.data.msg
    const data = response.data.data

    // 已下线、认证用户失败弹出登录提示
    if (code === 401) {
      Modal.error({
        title: '登录已过期',
        content: '很抱歉，登录已过期，请重新登录',
        okText: '重新登录',
        mask: false,
        onOk: () => {
          //跳转到登录页面
        }
      })
      return data
    }

    if (code !== 200) {
      notification.error({
        message: '系统提示',
        description: msg
      })
      return data
    }
    
    return data
  },
  error => {
    if (error.response) {
      const data = error.response.data
      switch (error.response.status) {
        case 403:
          notification.error({
            message: '系统提示',
            description: '拒绝访问',
            duration: 4
          })
          break
        case 500:
          notification.error({
            message: '系统提示',
            description: '网络似乎有点问题！',
            duration: 4
          })
          break
        case 404:
          notification.error({
            message: '系统提示',
            description: '很抱歉，资源未找到!',
            duration: 4
          })
          break
        case 504:
          notification.error({
            message: '系统提示',
            description: '网络超时'
          })
          break
        case 401:
          notification.error({
            message: '系统提示',
            description: '未授权，请重新登录',
            duration: 4
          })
          break
        default:
          notification.error({
            message: '系统提示',
            description: data.message,
            duration: 4
          })
          break
      }
    }
    return Promise.reject(error)
  }
)
/**
 * 封装过的axios
 */
export const http = service

/**
 * 封装过的axios.get
 */
export const get = service.get

/**
 * 封装过的axios.post
 */
export const post = service.post

export default {
  install(app: App): void {
    storage = app.config.globalProperties.$local
    app.config.globalProperties.$axios = service
  }
}
